package com.zhenyi.youqu.config;

import com.cenjiawen.common.ArpHandler;
import com.cenjiawen.common.Config;
import com.jfinal.config.Constants;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.render.FreeMarkerRender;

import freemarker.template.Configuration;

public class YQConfig extends Config {

	@Override
	public void configConstant(Constants me) {
		super.configConstant(me);
		Configuration cfg = FreeMarkerRender.getConfiguration();
		cfg.setDefaultEncoding("utf-8");
	}

	@Override
	public void configRoute(Routes me) {
		if (PropKit.getBoolean("EN_YOUQU", false)) {
			me.add(new YQRoutes());
		}
	}

	@Override
	protected void configSQLConnection(Plugins me) {
		// boolean debug = PropKit.getBoolean("devMode", false);
		if (PropKit.getBoolean("EN_YOUQU", false)) {
			String host = PropKit.get("YOUQU_DB", "192.168.50.222");
			String password = PropKit.get("YOUQU_DB_PWD", "zhenyi");
			addDataSource(host, "youqu", "root", password, me, new ArpHandler() {

				@Override
				public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
					com.youqu.common.model._MappingKit.mapping(arp);
					arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
					arp.addSqlTemplate("youqu/youqu.txt");
					arp.addSqlTemplate("youqu/youquAdmin.txt");
					arp.addSqlTemplate("youqu/youquLocation.txt");
					return arp;
				}
			});
		}
	}

	@Override
	public void configPlugin(Plugins me) {
		super.configPlugin(me);
		boolean debug = PropKit.getBoolean("devMode", false);
		if (!debug) {
			if (PropKit.getBoolean("EN_YOUQU", false)) {
				LogKit.info("yq Task");
				Cron4jPlugin jhCron = new Cron4jPlugin("/youqu/youquTask.txt", "youquTask");
				me.add(jhCron);
				LogKit.info("trigger youquTask");
			}
		}
	}

	@Override
	public void afterJFinalStart() {
		LogKit.info("Start YQ config");
	}
}
